<?php


namespace org;

use think\Controller;
use think\Db;
use think\Request;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use AlibabaCloud\Mts\Mts;

class AliSts extends Controller{
    private $config = [];
    public function __construct(Request $request = null)
    {
        $storage_config=Db::name('storage')->field('ali')
            ->where('acid',1)
            ->find();
        $this->config = json_decode($storage_config['ali'], true);
        $accessKeyId = $this->config['accessKeyId'];
        $accessKeySecret = $this->config['accessKeySecret'];
        AlibabaCloud::accessKeyClient($accessKeyId, $accessKeySecret)->regionId('cn-hangzhou')->asDefaultClient();
    }

    /**
     * 调用AssumeRole接口获取一个扮演该角色的临时身份
     * @return array
     */
    public function getSts($RoleSessionName = 'oss')
    {
        try {
            $result = AlibabaCloud::rpc()
                ->product('Sts')
                // ->scheme('https') // https | http
                ->version('2015-04-01')
                ->action('AssumeRole')
                ->method('POST')
                ->host('sts.aliyuncs.com')
                ->options([
                    'query' => [
                        'RegionId' => "cn-hangzhou",
                        'RoleArn' => "acs:ram::{$this->config['ram']['accountID']}:role/{$this->config['ram']['roleName']}",
                        'RoleSessionName' => $RoleSessionName,
                    ],
                ])
                ->request();
            return [0, $result->toArray()];
        } catch (ClientException $e) {
            return [-1, $e->getErrorMessage()];
        } catch (ServerException $e) {
            return [-1, $e->getErrorMessage()];
        }
    }

    /*
     * 转码
     * $Input 作业输入
     * $PipelineId 管道ID
     * $OutputBucket 输出路径
     * $Outputs 转码模板参数
     * */
    public function submitJobM3u8($Input,$PipelineId,$OutputBucket,$Outputs){
        try {
            $result = AlibabaCloud::rpc()
                ->product('Mts')
                // ->scheme('https') // https | http
                ->version('2014-06-18')
                ->action('SubmitJobs')
                ->method('POST')
                ->host('mts.aliyuncs.com')
                ->options([
                    'query' => [
                        'Input'=>$Input,
                        'PipelineId'=>$PipelineId,
                        'OutputBucket'=>$OutputBucket,
                        'Outputs'=>$Outputs,
                    ],
                ])
                ->request();
            //return [0, $result->toArray()];
            $res = $result->toArray();
            //return $res;
            return ['code' => 0, 'msg' => $res['JobResultList']['JobResult']['0']['Job']['JobId']];
        } catch (ClientException $e) {
            //return [-1, $e->getErrorMessage()];
            return ['code' => 1, 'msg' => $e->getErrorMessage()];
        } catch (ServerException $e) {
           // return [-1, $e->getErrorMessage()];
            return ['code' => 1, 'msg' => $e->getErrorMessage()];
        }
    }

    /*
     * 查询转码是否成功
     * */
    public function queryJobList($id){
        try {
            $result = AlibabaCloud::rpc()
                ->product('Mts')
                // ->scheme('https') // https | http
                ->version('2014-06-18')
                ->action('QueryJobList')
                ->method('POST')
                ->host('mts.aliyuncs.com')
                ->options([
                    'query' => [
                        'JobIds'=>$id
                    ],
                ])
                ->request();
            //return [0, $result->toArray()];
            $res = $result->toArray();
            //return $res;
            if(isset($res['JobList']['Job'][0]['FinishTime'])){
                return ['code' => 0, 'msg' => array(
                    'code'=>0,
                    'data'=>$res,
                    'items'=>$res['JobList']['Job'][0]['Output']['OutputFile']['Object']
                )];
            }else{
                return ['code' => 0, 'msg' => array(
                    'code'=>1,
                    'data'=>$res,
                    'items'=>$res['JobList']['Job'][0]['Output']['OutputFile']['Object']
                )];
            }

        } catch (ClientException $e) {
            return [-1, $e->getErrorMessage()];
        } catch (ServerException $e) {
            return [-1, $e->getErrorMessage()];
        }
    }

    /*
     * 创建水印模板
     * $Input 模板名称。 最大长度128字节。
     * $PipelineId 水印模板配置。 JSON对象
     * */
    public function waterMarkTemplate($name,$config){
        try {
            $result = AlibabaCloud::rpc()
                ->product('Mts')
                // ->scheme('https') // https | http
                ->version('2014-06-18')
                ->action('AddWaterMarkTemplate')
                ->method('POST')
                ->host('mts.aliyuncs.com')
                ->options([
                    'query' => [
                        'Name'=>$name,
                        'Config'=>$config,
                    ],
                ])
                ->request();
            return [0, $result->toArray()];
        } catch (ClientException $e) {
            return [-1, $e->getErrorMessage()];
        } catch (ServerException $e) {
            return [-1, $e->getErrorMessage()];
        }
    }

    /*
     * 修改水印模板
     * */
    public function updateWaterMarkTemplate($WaterMarkTemplateId,$name,$config){
        try {
            $result = AlibabaCloud::rpc()
                ->product('Mts')
                // ->scheme('https') // https | http
                ->version('2014-06-18')
                ->action('UpdateWaterMarkTemplate')
                ->method('POST')
                ->host('mts.aliyuncs.com')
                ->options([
                    'query' => [
                        'WaterMarkTemplateId'=>$WaterMarkTemplateId,
                        'Name'=>$name,
                        'Config'=>$config,
                    ],
                ])
                ->request();
            return [0, $result->toArray()];
        } catch (ClientException $e) {
            return [-1, $e->getErrorMessage()];
        } catch (ServerException $e) {
            return [-1, $e->getErrorMessage()];
        }
    }

    /*
     * 创建转码模板
     * $name 名称
     * $container 容器
     * $video JSON对象
     * */
    public function templateResponse($name,$container,$video){
        try {
            $result = AlibabaCloud::rpc()
                ->product('Mts')
                // ->scheme('https') // https | http
                ->version('2014-06-18')
                ->action('AddTemplate')
                ->method('POST')
                ->host('mts.aliyuncs.com')
                ->options([
                    'query' => [
                        'Name'=>$name,
                        'Container'=>$container,
                        'Video'=>$video,
                    ],
                ])
                ->request();
            return [0, $result->toArray()];
        } catch (ClientException $e) {
            return [-1, $e->getErrorMessage()];
        } catch (ServerException $e) {
            return [-1, $e->getErrorMessage()];
        }
    }

    /*
     * 搜索管道
     * */
    public function searchPipeline(){
        try {
            $result = AlibabaCloud::rpc()
                ->product('Mts')
                // ->scheme('https') // https | http
                ->version('2014-06-18')
                ->action('SearchPipeline')
                ->method('POST')
                ->host('mts.aliyuncs.com')
                ->options([])
                ->request();
            return [0, $result->toArray()];
        } catch (ClientException $e) {
            return [-1, $e->getErrorMessage()];
        } catch (ServerException $e) {
            return [-1, $e->getErrorMessage()];
        }
    }
}